/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package terror;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

/**
 * @author OAKLAND
 */
public class TerrorMIDPCanvas extends GameCanvas implements CommandListener, Runnable {

    /**
     * constructor
     */
    Thread hilo;
    Graphics g;
    
    int coorX=5; //coordenada del jugador en x
    int coorY=100; //coordenada del jugador en y
    int dx=3;
    int dy=3;
    int tmp;
    private Sprite dollAction;
    Player pbg;
    String mensaje = "";
    String mensaje2 = "";
    boolean pressed=false;

     //Metodo para reproducir el sonido de fondo
    public void playsndBg(String media) {
        try {
        InputStream is = getClass().getResourceAsStream(media);
        pbg = Manager.createPlayer(is, "audio/midi");
        //Indica que se debe reproducir en un loop 
        pbg.setLoopCount(-1);
        pbg.prefetch();
        pbg.start();
        } catch (IOException ex) {
        } catch (MediaException ex) {
    } }
    
    public Sprite getDollAction() {
        return dollAction;
    }

    public void setDollAction(Sprite dollAction) {
        this.dollAction = dollAction;
    }
    Sprite back,front,left,right,closet,table,front_wall, floor, text;
    
    public TerrorMIDPCanvas() {
        super(true);
        g=getGraphics();
        try {
            // Set up this canvas to listen to command events
            setCommandListener(this);
            tmp=0;
            back = new Sprite(Image.createImage("/Graphics/back.png"),11,16);
            front = new Sprite(Image.createImage("/Graphics/front.png"),11,16);
            left = new Sprite(Image.createImage("/Graphics/left.png"),9,16);
            right = new Sprite(Image.createImage("/Graphics/right.png"),9,16);
            closet = new Sprite(Image.createImage("/Graphics/closet.png"),21,18);
            table = new Sprite(Image.createImage("/Graphics/table.png"),19,28);
            front_wall=new Sprite(Image.createImage("/Graphics/frontwall.png"),128,160);
            floor = new Sprite(Image.createImage("/Graphics/floor.png"),127,158);
            text = new Sprite(Image.createImage("/Graphics/text.png"),126,42);
            // Add the Exit command
            addCommand(new Command("Exit", Command.EXIT, 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        hilo=new Thread(this);
        hilo.start();
        playsndBg("/sounds/creppy.mid");
    }

    
    
    public int getCoorX(int x){
        return(x*getWidth())/100;
    }
    public int getCoorY(int y){
        return (y*getHeight())/100;
    }
    public void run(){
        while(true){
            eventoTecla();
            paint();
           
        }
    }
    public void paint(){
        paintLayer();
        paintSprite();
    }
    public void paintLayer(){
        g.setColor(0x000001);
        g.fillRect(getCoorX(0), getCoorY(0), getCoorX(100), getCoorY(100));
    }
   
    public void paintSprite(){
        floor.setPosition(getCoorX(0), getCoorY(0));
        floor.paint(g);
        front_wall.setPosition(getCoorX(0),getCoorY(0));
        front_wall.paint(g);
        closet.setPosition(getCoorX(5),getCoorY(20));
        closet.paint(g);
        table.setPosition(getCoorX(50)-(table.getWidth()/2), getCoorY(50)-(table.getHeight()/2));
        table.paint(g);
        getDollAction().setPosition(getCoorX(coorX), getCoorY(coorY)-back.getHeight());
        getDollAction().paint(g);
        getDollAction().nextFrame();
        if(pressed){
            text.setPosition(getCoorX(0), getCoorY(68));
            text.paint(g);
        }
        g.drawString(mensaje, getCoorX(5), getCoorY(73), 0);  
        g.drawString(mensaje2, getCoorX(5), getCoorY(83), 0);
        try {
            hilo.sleep(100);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        flushGraphics();
    }
    
    public void mostrar_mensaje(){
        try {
            paintSprite();
            hilo.sleep(3000);
            pressed=false;
            mensaje = "";
            mensaje2 = "";
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    public void eventoTecla(){
        int tecla = getKeyStates();
        switch(tecla){
            case 0:
                if(tmp==0){
                    setDollAction(back);
                    dollAction.setFrame(0);
                    tmp=1;
                    break;  
                  }
                else{
                    break;
                }
                                  
                
            case UP_PRESSED:
                setDollAction(front);
                if (getCoorY(coorY)>getCoorY(16)+dollAction.getHeight()){
                  if(getDollAction().collidesWith(table, true)){
                      
                  }
                  else if(getDollAction().collidesWith(closet, true)){
                      
                  }
                  else
                      coorY-=dy; 
                }
                    
                dollAction.nextFrame();
                break;
                
            case DOWN_PRESSED:
                setDollAction(back);
                if(getCoorY(coorY)<getCoorY(100))
                   if(getDollAction().collidesWith(table, true)){
                      
                  }
                  else if(getDollAction().collidesWith(closet, true)){
                      
                  }
                  else
                        coorY+=dy;
                dollAction.nextFrame();
                break;
                
            case LEFT_PRESSED:
                setDollAction(left);
                if(getCoorX(coorX)>getCoorX(5))
                   if(getDollAction().collidesWith(table, true)){
                      
                  }
                  else if(getDollAction().collidesWith(closet, true)){
                      
                  }
                  else
                        coorX-=dx;
                dollAction.nextFrame();
                break;
                
            case RIGHT_PRESSED:
                setDollAction(right);
                if(getCoorX(coorX)<getCoorX(95)-dollAction.getWidth())
                    if(getDollAction().collidesWith(table, true)){
                      
                  }
                  else if(getDollAction().collidesWith(closet, true)){
                      
                  }
                  else
                        coorX+=dx;
                dollAction.nextFrame();
                break;
            
            case 256:
                if(getDollAction().collidesWith(table, true)){
                    mensaje="Table...";
                    pressed=true;
                    mostrar_mensaje();
                }
                else if(getDollAction().collidesWith(closet, true)){
                    mensaje="Closet...";
                    pressed=true;
                    mostrar_mensaje();
                }
                else if(getCoorY(coorY)<getCoorY(16)+dollAction.getHeight()&&getCoorX(coorX)<getCoorX(80)-dollAction.getWidth()&&getCoorX(coorX)>getCoorX(60)){
                    mensaje = "The door is closed. The";
                    mensaje2 = "key should be here.";
                    pressed = true;
                    mostrar_mensaje();
                }
                else{
                    mensaje = "I need to get out";
                    mensaje2 = "of here";
                    pressed=true;
                    mostrar_mensaje();
                }
                break;
                        
        }
    }
    protected void keyPressed(int keyCode) {
    }

    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyCode) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyCode) {
    }

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {
    }

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {
    }

    /**
     * Called when action should be handled
     */
    public void commandAction(Command command, Displayable displayable) {
    }
}
